﻿using CPPEI.ETL.DataServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.DSImplement.WS
{
    public class ArrayData : IData
    {
        /// <summary>
        /// 从数据类型里获取列的信息
        /// </summary>
        public static List<Column> GetColumns(Type elementType, string rootPath)
        {
            List<Column> columns = new List<Column>();
            List<string> propertyPaths = null;
            List<Type> propertyTypes = null;
            CPPEI.ETL.Infrastructure.PropertyUtil.GetPropertyPathList(elementType, rootPath, out propertyPaths, out propertyTypes);

            for (int index = 0; index < propertyPaths.Count; index++)
            {
                Column column = new Column(propertyPaths[index].Split(new char[] { '.' }).Last(), propertyPaths[index], propertyTypes[index]);

                columns.Add(column);
            }
            return columns;
        }

        public DataType Type
        {
            get;
            private set;
        }

        public object Data
        {
            get;
            private set;
        }

        private object _object;

        private List<Column> _columns = null;

        private Type _itemType;
        private string _rootPath;
        /// <summary>
        /// 在这个对象上含有数组属性，但是这个对象可能是别的对象的属性
        /// </summary>
        private object[] _objectArr;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="rootPath">从哪一个位置开始有数组属性，格式（xxx.xxxx.xxx)</param>
        public ArrayData(object data, string rootPath)
        {
            Type = DataType.Array;
            Data = data;
            _object = data;
            _rootPath = rootPath;
            _objectArr = (object[])WSTypeUtil.GetFieldValue(data, rootPath);
            if (_objectArr != null)
            {
                _itemType = _objectArr.GetType().GetElementType();
            }
        }

        public List<Column> GetSchema()
        {
            if (_columns == null)
            {
                _columns = GetColumns(_itemType, _rootPath);
            }
            return _columns;
        }
        public int RowCount
        {
            get
            {
                if (_objectArr == null)
                {
                    return 0;
                }
                return _objectArr.Length;
            }
        }

        public Row Get(int index)
        {
            List<Column> columns = GetSchema();

            Row row = new Row(index);
            List<string> paths = null;
            List<object> values = null;

            object value = _objectArr[index];
            CPPEI.ETL.Infrastructure.PropertyUtil.GetPropertyPathValueList(value.GetType(), value, _rootPath, out paths, out values);

            for (int colIndex = 0; colIndex < columns.Count; colIndex++)
            {
                if (values[colIndex] != null)
                {
                    Row.Add(row, columns[colIndex].Name, values[colIndex].ToString());
                }
                else
                {
                    Row.Add(row, columns[colIndex].Name, null);
                }
            }

            return row;
        }
    }
}
